package httpproxy.utils;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * HTTP 网络请求工具类
 * Created by fancr on 2017-02-15.
 */

public class HttpUtils {

    private final static String TAG = "HttpUtils:";

    private final static String BASE_URL = "http://10.200.133.25/";

    private HttpUtils(){

    }

    /**
     * http get请求
     * @param path
     * @param params
     * @return
     */
   public static ResultData getHttp(String path, Map<String,Object> params){
       ResultData result = new ResultData();
             HttpClient client = new HttpClient();
             GetMethod getMethod = new GetMethod(path);
             HttpMethodParams methodParams = new HttpMethodParams();
             if(params != null && params.size() > 0){
                 for (Map.Entry<String,Object> param: params.entrySet()
                      ) {
                      methodParams.setParameter(param.getKey(),param.getValue());
                 }
             }
             getMethod.setParams(methodParams);
       try {
           client.executeMethod(getMethod);
           result.setCode(getMethod.getStatusCode());
           result.setData(getMethod.getResponseBodyAsString());
       } catch (IOException e) {
           result.setCode(0);
       }
       return result;
    }

    /**
     * http post请求
     * @param path
     * @param params
     * @return
     */
    public static ResultData postHttp(String path, Map<String,Object> params){
        ResultData result = new ResultData();
        HttpClient client = new HttpClient();
        PostMethod postMethod = new PostMethod(path);
        NameValuePair[] nameValuePairses = new NameValuePair[params.size()];
        int i = 0;
        if(params != null && params.size() > 0){
            for (Map.Entry<String,Object> param: params.entrySet()
                    ) {
                NameValuePair pair = new NameValuePair();
                 pair.setName(param.getKey());
                 pair.setValue(param.getValue().toString());
                nameValuePairses[i] =  pair;
                        ++i;
            }
        }
        postMethod.setRequestBody(nameValuePairses);
        try {
            client.executeMethod(postMethod);
            result.setCode(postMethod.getStatusCode());
            result.setData(postMethod.getResponseBodyAsString());
        } catch (IOException e) {
            result.setCode(0);
        }
        return result;
    }

    /**
     * 日期转化为时间字符串
     * @param input
     * @return   yyyy-MM-dd 格式 如（2017-02-14）
     */
    public static String dateToDay(Date input){
        if(input == null){
            input = new Date();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr = dateFormat.format(input);
        return dateStr;
    }
}
